#pragma once

#include "OISEvents.h"
#include "OISInputManager.h"
#include "OISMouse.h"
#include "OISKeyboard.h"
#include "OISJoystick.h"
extern bool shutDown;


class InputHandler : 
	public OIS::MouseListener, 
	public OIS::KeyListener, 
	public OIS::JoyStickListener
{
private:
	OIS::InputManager *m_ois;
	OIS::Mouse *mMouse;
	OIS::Keyboard *mKeyboard;
	unsigned long m_hWnd;
public:
	InputHandler(unsigned long hWnd); 
	~InputHandler();

	void setWindowExtents(int width, int height) ;
	void capture();

	// MouseListener
	bool mouseMoved(const OIS::MouseEvent &evt);
	bool mousePressed(const OIS::MouseEvent &evt, OIS::MouseButtonID);
	bool mouseReleased(const OIS::MouseEvent &evt, OIS::MouseButtonID);

	// KeyListener
	bool keyPressed(const OIS::KeyEvent &evt);
	bool keyReleased(const OIS::KeyEvent &evt);

	// JoyStickListener
	bool buttonPressed(const OIS::JoyStickEvent &evt, int index);
	bool buttonReleased(const OIS::JoyStickEvent &evt, int index);
	bool axisMoved(const OIS::JoyStickEvent &evt, int index);
	bool povMoved(const OIS::JoyStickEvent &evt, int index);

	bool	up;
	bool	down;
	bool	left;
	bool	right;
};

#include "OgreStringConverter.h"

InputHandler::InputHandler(unsigned long hWnd)  {

	OIS::ParamList pl;
	pl.insert(OIS::ParamList::value_type("WINDOW", Ogre::StringConverter::toString(hWnd)));

	m_hWnd = hWnd;
	m_ois = OIS::InputManager::createInputSystem( pl );
	mMouse = static_cast<OIS::Mouse*>(m_ois->createInputObject( OIS::OISMouse, true ));
	mKeyboard = static_cast<OIS::Keyboard*>(m_ois->createInputObject( OIS::OISKeyboard, true));
	mMouse->setEventCallback(this);
	mKeyboard->setEventCallback(this);
	up=down=left=right = false;
}

InputHandler::~InputHandler() {
	if (mMouse)
		delete mMouse;
	if (mKeyboard)
		delete mKeyboard;
	OIS::InputManager::destroyInputSystem(m_ois);
}

void InputHandler::capture() {
	mMouse->capture();
	mKeyboard->capture();
}

void  InputHandler::setWindowExtents(int width, int height){
	//Set Mouse Region.. if window resizes, we should alter this to reflect as well
	const OIS::MouseState &ms = mMouse->getMouseState();
	ms.width = width;
	ms.height = height;
}


// MouseListener
bool InputHandler::mouseMoved(const OIS::MouseEvent &evt) {
	return true;
}

bool InputHandler::mousePressed(const OIS::MouseEvent &evt, OIS::MouseButtonID btn) {
	return true;
}

bool InputHandler::mouseReleased(const OIS::MouseEvent &evt, OIS::MouseButtonID btn) {
	return true;
}


// KeyListener
bool InputHandler::keyPressed(const OIS::KeyEvent &evt) {
	if(evt.key == OIS::KC_UP)
		up = true;
	if(evt.key == OIS::KC_DOWN)
		down = true;
	if(evt.key == OIS::KC_LEFT)
		left = true;
	if(evt.key == OIS::KC_RIGHT)
		right = true;
	return true;
}

bool InputHandler::keyReleased(const OIS::KeyEvent &evt) {
	if (evt.key == OIS::KC_ESCAPE)
		shutDown = true;
	if(evt.key == OIS::KC_UP)
		up = false;
	if(evt.key == OIS::KC_DOWN)
		down = false;
	if(evt.key == OIS::KC_LEFT)
		left = false;
	if(evt.key == OIS::KC_RIGHT)
		right = false;
	return true;
}



// JoyStickListener
bool InputHandler::buttonPressed(const OIS::JoyStickEvent &evt, int index) {
	return true;
}

bool InputHandler::buttonReleased(const OIS::JoyStickEvent &evt, int index) {
	return true;
}

bool InputHandler::axisMoved(const OIS::JoyStickEvent &evt, int index) {
	return true;
}

bool InputHandler::povMoved(const OIS::JoyStickEvent &evt, int index) {
	return true;
}
